
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "ROGUE" or not module then return end

local loaded = false
local ComboSkill = 45 -- 주력 콤보스킬 기력
local FinishSkill = 35 -- 주력 마무리스킬 기력

local L_MUTILATE = "Mutilate"
local L_SWITCH_COMBO_POSITION = "Switch Combo count position"
local L_SWITCH_COMBO_POSITION_TT = "Switch Combo count position"
local L_SWITCH_COMBO_POSITION_PLAYER = "set by Player"
local L_SWITCH_COMBO_POSITION_TARGET = "set by Target"
if GetLocale()=="koKR" then
L_MUTILATE = "절단"
L_SWITCH_COMBO_POSITION = "연계수치 위치 설정"
L_SWITCH_COMBO_POSITION_TT = "연계수치의 위치를 플레이어 또는 대상 옆으로 설정합니다."
L_SWITCH_COMBO_POSITION_PLAYER = "플레이어옆"
L_SWITCH_COMBO_POSITION_TARGET = "대상옆"
end

module.config = {}

function module:init()
	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("UNIT_LEVEL")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")
	self:update()

	if loaded==false then
		self:AddMiscConfig{
			type = "radio",
			text = L_SWITCH_COMBO_POSITION,
			tooltip = L_SWITCH_COMBO_POSITION_TT,
			values = {
				{
					text = L_SWITCH_COMBO_POSITION_PLAYER,
					value = "player",
				},
				{
					text = L_SWITCH_COMBO_POSITION_TARGET,
					value = "target",
				},
			},
			get = function ()
				return module.addon.db.rg_combo_position or "target"
			end,
			set = function (value)
				module.addon.db.rg_combo_position = value
			end,
			col = 2,
			disable = not module.addon.db.useModule,
		}
	end
	loaded = true
end

function module:update()
	local isKnown
	self.primary = GetSpecialization()
	for auraName, t in pairs(self.config) do
		if t.talent and t.talent > 0 then
			local name, iconTexture, tier, column, selected, available = GetTalentInfo(t.talent);
			if name and available then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		else
			self.config[auraName].use = false
			if IsPlayerSpell then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		end
	end

	if self.primary == 1 then
		_, _, _, ComboSkill = GetSpellInfo(1329) -- 절단
	elseif self.primary == 2 then
		_, _, _, ComboSkill = GetSpellInfo(1752) -- 사악한 일격
	elseif self.primary == 3 then
		_, _, _, ComboSkill = GetSpellInfo(16511) -- 과다출혈
	end
end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
function module:UNIT_LEVEL(unit)
	if unit=="player" then
		self:update()
	end
end
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end

function module:getComboText()
	local combo = GetComboPoints("player")
	local comboText, r, g, b = "", 1.0, 0.5, 0.1

	if( combo <= 0 )then
		return ""
	end

	if ( UnitPowerType("player") == 3 ) then -- energy
		local mana = UnitMana("player")
		if( mana >= ComboSkill )then
			r = 0.1
			g = 1.0
			b = 0.1 -- green
		elseif( mana >= FinishSkill )then
			r = 0.0
			g = 0.39
			b = 0.88 -- blue
		else
			r = 1.0
			g = 0.1
			b = 0.1 -- red
		end
	end
	return (":|cff%02x%02x%02x%d|r"):format(r * 255, g * 255, b * 255, combo)
end

function module:getPlayerText()
	if module.addon.db.rg_combo_position == "player" then
		local text = self:getComboText()
		return text
	end
end

function module:getTargetText()
	if not module.addon.db.rg_combo_position or module.addon.db.rg_combo_position == "target" then
		local text = self:getComboText()
		return text
	end	
end

